package nil.ed.lazy.media;

import nil.ed.lazy.media.exception.VideoResolveException;
import nil.ed.lazy.media.model.VideoBasicInfo;
import org.bytedeco.javacv.FFmpegFrameGrabber;

/**
 * @author lidelin
 * @since 1.0
 */
public class VideoUtils {

    /**
     * 解析URL源的视频
     * @param videoUrl 视频URL
     * @return 视频解析结果
     * @throws VideoResolveException 视频解析失败异常，封装了{@link FFmpegFrameGrabber.Exception}
     */
    public static VideoBasicInfo resolveVideoUrl(String videoUrl) throws VideoResolveException {
        /*
        FFmpegFrameGrabber非线程安全
         */
        try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoUrl)) {
            grabber.start();
            VideoBasicInfo videoBasicInfo = new VideoBasicInfo();
            videoBasicInfo.setWidth(grabber.getImageWidth());
            videoBasicInfo.setHeight(grabber.getImageHeight());
            videoBasicInfo.setBitrate(grabber.getVideoBitrate());
            videoBasicInfo.setFrameRate(grabber.getFrameRate());
            return videoBasicInfo;
        } catch (Exception e) {
            throw new VideoResolveException(e);
        }
    }

}
